local FileUtil = require("file/file_util")

local ngx_re_gmatch = ngx.re.gmatch
local ngx_re_find = ngx.re.find
local lrucache = require "resty.lrucache"
local resolver = require "resty.dns.resolver"
local cache_storage = lrucache.new(200)-----创建本地缓存
---
local ok, new_tab = pcall(require, "table.new")

if not ok then
  new_tab = function (narr, nrec) return {} end
end
--创建dns table
local _dns_servers = new_tab(5, 0)
local _M = {
    _VERSION = '0.14',
}
function _M.init_dns_server()
    local text = FileUtil._read_file_data('/etc/resolv.conf')

    local captures, it, err
    it, err = ngx_re_gmatch(text, [[^nameserver\s+(\d+?\.\d+?\.\d+?\.\d+$)]], "jomi")
    --judge the domain is complete domain

    --   if not ngx.re.find("haha",'\\.') then
    --     ngx.log(ngx.INFO,domain.." domain is not complete domain")


    local dns_search ,search_it,search_err
    search_it,search_err =  ngx_re_gmatch(text, [[^search\s+(\w+\.\w+\.\w+\.\w+)+]], "jomi")


    for dns_search, search_err in search_it do
        if not search_err then  ---将dns 地址缓存在table中  需要实现init worker by lua
            -- ngx.shared.DNS:set(#_dns_servers + 1,captures[1]);
            --_dns_servers[tostring(#_dns_servers + 1)] = captures[1]
            ngx.log(ngx.INFO,"find search domain"..dns_search[1])
            ngx.shared.DNS:set("affix",dns_search[1]);
        end
        --end

    end
    for captures, err in it do
        if not err then  ---将dns 地址缓存在table中  需要实现init worker by lua
        ngx.shared.DNS:set(#_dns_servers + 1,captures[1]);
            --_dns_servers[tostring(#_dns_servers + 1)] = captures[1]

        end
    end

end





----判断是否是ip地址
function _M:_is_addr (hostname)
    return ngx_re_find(hostname, [[\d+?\.\d+?\.\d+?\.\d+$]], "jo")
end

--if ngx.shared.DNS:get(1)==nil then
--    ngx.log(ngx.INFO,"-----ngx.shared.DNS.get(0) 为空 ")
--else
--    ngx.log(ngx.INFO,"-----打印 ngx.shared.DNS.get(0) "..(ngx.shared.DNS:get(1)))
--end

function  _M: _get_addr (hostname)
    if self:_is_addr(hostname) then
        return hostname, hostname
    end

    ngx.log(ngx.INFO,"-----判断是域名 就从缓存中获取看看")
    -----如果不是ip地址 就从缓存中获取看看
    local addr = cache_storage:get(hostname)
    ngx.log(ngx.INFO,"----- -----获取到了就返回")
    -----获取到了就返回
    if addr then
        ngx.log(ngx.INFO,"----- -----获取到了直接返回")
        return addr, hostname
    end
    -----没有从本地的dns解析获取到 就从外部的dns服务器上去获取看看
    ngx.log(ngx.INFO,"去找缓存中的dns server"..ngx.shared.DNS:get(1))
    local dnsServer = ngx.shared.DNS:get(1);
    if not dnsServer then
        dnsServer ='8.8.8.8'
    end
    local r, err = resolver:new({
        nameservers = {dnsServer},
        retrans = 5,  -- 5 retransmissions on receive timeout
        timeout = 2000,  -- 2 sec
    })

    if not r then
        return nil, hostname
    end
    local modiffy_hostname=hostname;
    if not ngx.re.find(hostname,'\\.') then
        ngx.log(ngx.INFO,hostname.." domain is not complete domain")
        modiffy_hostname= hostname.."."..ngx.shared.DNS:get("affix")
    end

    -------开始dns 查询
    local answers, err = r:query(modiffy_hostname, {qtype = r.TYPE_A})
    -------如果没有查到返回为空
    if not answers or answers.errcode then
        ngx.log(ngx.INFO,没有找到域名对应ip)
        return nil, hostname
    end
    --如果查到了 解析里面的ip地址值
    ngx.log(ngx.INFO,"查到了 开始解析里面的ip地址值")
    for i, ans in ipairs(answers) do
        if ans.address then
            cache_storage:set(hostname, ans.address, 300)
            ngx.log(ngx.INFO,"find realip"..ans.address)
            ------缓存住然后返回结果
            return ans.address, hostname
        end
    end

    return nil, hostname
end


return _M
